<template>
  <div class="edit-form">
    <FormBasicEquipment
      :submit-handle="submitHandle"
      :data-model="deviceBasic.totalForm"
    />
  </div>
</template>

<script>
import data from './const'
import FormBasicEquipment from './form1.vue'
import { mapState, mapMutations } from 'vuex'
import { deepClone, deepCopy } from '@/utils/index'
// const  data2=require("./const")
// const data =deepClone(data1)
export default {
  name: 'EditBasicEquipment',
  components: { FormBasicEquipment },

  data() {
    // 组装扩展信息

    return {
      data: deepCopy(data), // JSON.parse(JSON.stringify(data)),
      submitHandle: params => {
        this.submitForm(params)
      }
    }
  },
  created() {
    this.$store.dispatch('deviceBasic/setTotalForm', this.data.totalForm)
  },
  mounted() {
    // 判断是编辑还是新增
    this.preloadData()
  },
  computed: {
    ...mapState(['deviceBasic'])
  },
  methods: {
    async preloadData() {
      const dataKey = this.$route.fullPath
      const id = localStorage.getItem(dataKey)
      if (id) {
        const data = await this.$store.dispatch(
          'deviceBasic/requestDeviceEditInfo',
          id
        )
        console.log(data, '编辑设备台账')
        // 把详情对象分拆为模板格式

        this.data.totalForm[0].formModel = data
        this.data.totalForm[1].formModel = data.extra
          ? JSON.parse(data.extra)
          : this.data.totalForm[1].formModel
        this.data.totalForm[2].formModel.archives = data.archives
        this.$store
          .dispatch(this.$store_url_button, {
            add: this.add || '',
            delete: this.delete || '',
            edit: this.edit || '',
            detail: this.detail || '',
            using: this.using || '',
            useless: this.useless || '',
            downloadIdentify: this.downloadIdentify || ''
          })
          .then(() => {
            // this.getList();
          })
      } else {
        // 先把常量信息给vuex

        this.$store
          .dispatch(this.$store_url_button, {
            add: this.add || '',
            delete: this.delete || '',
            edit: this.edit || '',
            detail: this.detail || '',
            using: this.using || '',
            useless: this.useless || '',
            downloadIdentify: this.downloadIdentify || ''
          })
          .then(() => {
            this.getList() // 表格数据
          })
      }
    },
    submitForm(params) {
      const data = Object.assign(
        {},
        params[0].formModel,
        { extra: JSON.stringify(params[1].formModel) },
        params[2].formModel
      )

      this.$store
        .dispatch('deviceBasic/saveDeviceInfo', data)
        .then(response => {
          this.$store.dispatch('tagsView/delVisitedView', {
            path: this.$route.fullPath
          })
          this.$router.push('/device-basic/index')
        })
    },
    getList() {
      this.$store.dispatch('deviceBasic/requestDeviceBroadInfo', true)
    }
  }
}
</script>
<style lang="scss">
.edit-form {
  margin: 10px;
  .edit-now-info {
    background: white;
  }
  .edit-form-history-info {
    background: white;
    margin: 20px 0;
  }
}
</style>
